import { Sprite, Texture } from 'pixi.js';
import { Body, World } from 'matter-js';
import { Entity, EntityConfig } from './Entity';

export interface ObstacleConfig extends EntityConfig {
  health?: number;
  isDestructible?: boolean;
  dropChance?: number; // 掉落物品的概率 (0-1)
  breakAnimationDuration?: number;
}

export class Obstacle extends Entity {
  private health: number;
  private isDestructible: boolean;
  private dropChance: number;
  private isDestroyed: boolean;
  private maxHealth: number;
  private breakAnimationTimer: number;
  private breakAnimationDuration: number;
  private damageState: number; // 0-3表示破损程度

  constructor(config: ObstacleConfig) {
    super(config);
    
    // 初始化障碍物属性
    this.health = config.health || 50;
    this.maxHealth = this.health;
    this.isDestructible = config.isDestructible !== undefined ? config.isDestructible : true;
    this.dropChance = config.dropChance || 0.3; // 默认30%的掉落概率
    this.isDestroyed = false;
    this.breakAnimationDuration = config.breakAnimationDuration || 500; // 默认500ms
    this.breakAnimationTimer = 0;
    this.damageState = 0;
    
    // 添加障碍物标签
    this.addTag('obstacle');
  }

  // 受到伤害
  public takeDamage(amount: number): boolean {
    if (!this.isDestructible || this.isDestroyed) {
      return false;
    }
    
    this.health -= amount;
    
    // 更新破损状态
    const healthPercentage = this.health / this.maxHealth;
    const newDamageState = Math.floor((1 - healthPercentage) * 3);
    
    if (newDamageState !== this.damageState) {
      this.damageState = newDamageState;
      this.updateAppearance();
    }
    
    if (this.health <= 0) {
      this.startBreakAnimation();
      return true; // 返回true表示障碍物被摧毁
    }
    
    return false;
  }

  // 更新外观
  private updateAppearance(): void {
    if (!this.sprite) return;
    
    // 根据破损程度改变外观
    const alpha = 1 - (this.damageState * 0.2); // 随着破损程度增加而变得更透明
    this.sprite.alpha = alpha;
    
    // 可以添加裂纹贴图或其他视觉效果
    const tint = 0x8B4513 - (this.damageState * 0x111111); // 随着破损程度增加而变暗
    this.sprite.tint = tint;
  }

  // 开始破坏动画
  private startBreakAnimation(): void {
    this.breakAnimationTimer = this.breakAnimationDuration;
  }

  // 检查是否应该掉落物品
  public shouldDropItem(): boolean {
    return this.isDestroyed && Math.random() < this.dropChance;
  }

  // 更新障碍物状态
  public update(delta: number): void {
    if (this.breakAnimationTimer > 0) {
      this.breakAnimationTimer -= delta;
      
      if (this.sprite) {
        // 播放破坏动画
        const progress = this.breakAnimationTimer / this.breakAnimationDuration;
        this.sprite.alpha = progress;
        this.sprite.scale.set(1 + (1 - progress) * 0.2); // 轻微膨胀效果
        
        // 添加旋转效果
        this.sprite.rotation += (1 - progress) * 0.1;
      }
      
      if (this.breakAnimationTimer <= 0) {
        this.destroy(); // 动画结束后销毁
      }
    }
  }

  // 销毁实例
  public destroy(): void {
    if (this.isDestroyed) return;
    
    this.isDestroyed = true;
    this.deactivate();
    
    if (this.sprite && !this.sprite.destroyed) {
      this.sprite.destroy();
    }
    if (this.body && this.body.world) {
      World.remove(this.body.world, this.body);
    }
  }

  // 获取当前生命值
  public getHealth(): number {
    return this.health;
  }

  // 获取破损状态
  public getDamageState(): number {
    return this.damageState;
  }

  // 检查是否可破坏
  public isDestructibleObstacle(): boolean {
    return this.isDestructible;
  }

  // 获取破坏动画进度
  public getBreakAnimationProgress(): number {
    if (this.breakAnimationTimer <= 0) return 0;
    return this.breakAnimationTimer / this.breakAnimationDuration;
  }
}